package com.upb.webserver.order;

import com.upb.webserver.account.AccountService;
import com.upb.webserver.common.pojo.account.AccountCommon;
import com.upb.webserver.common.pojo.base.BasePageResponse;
import com.upb.webserver.common.pojo.base.BaseResponse;
import com.upb.webserver.common.pojo.host.RspHostVO;
import com.upb.webserver.common.pojo.order.ReqCloseOrderVO;
import com.upb.webserver.common.pojo.order.RspHeaderOfWorkOrderVO;
import com.upb.webserver.common.pojo.order.RspOrgOfWorkerOrderVO;
import com.upb.webserver.common.pojo.org.RspOrgVO;
import com.upb.webserver.common.returncode.ConstantCode;
import com.upb.webserver.common.tools.JsonUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * work order api, support close order
 */
@Log4j2
@RestController
@RequestMapping("work-order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private WorkOrderManager workOrderManager;
    @Autowired
    private AccountService accountService;


    /**
     * close an order
     */
    @PatchMapping("close")
    public BaseResponse closeWorkOrder(@RequestBody @Valid ReqCloseOrderVO param, HttpServletRequest request) {
        log.info("start exec method [closeWorkOrder]. param:{}", JsonUtils.objToString(param));
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        orderService.closeWorkOrder(accountCommon.getPlatform(), accountCommon.getId(), param);
        log.info("success exec method [closeWorkOrder]");
        return new BaseResponse(ConstantCode.SUCCESS);
    }

    /**
     * @param pageNumber
     * @param pageSize
     * @param type
     * @param statusList
     * @param request
     * @return
     */
    @GetMapping(value = "list")
    public BasePageResponse queryWorkOrderByPage(@RequestParam("pageNumber") Integer pageNumber,
                                                 @RequestParam("pageSize") Integer pageSize,
                                                 @RequestParam(value = "type", required = false) Byte type,
                                                 @RequestParam(value = "status", required = false, defaultValue = "") List<Byte> statusList, HttpServletRequest request) {
        log.info("start exec method [queryWorkOrderByPage]. pageNumber:{} pageSize:{} statusSet:{}", pageNumber, pageSize, JsonUtils.objToString(statusList));
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        BasePageResponse response = orderService.queryWorkOrderByPage(accountCommon.getPlatform(), accountCommon.getId(), type, statusList, pageNumber, pageSize);
        log.info("success exec method [queryWorkOrderByPage]. result:{}", JsonUtils.objToString(response));
        return response;
    }


    /**
     * @param workOrderId
     * @param request
     * @return
     */
    @GetMapping(value = "/{workOrderId}/self-org")
    public BaseResponse getSelfOrgFromWorkOrder(@PathVariable("workOrderId") Integer workOrderId, HttpServletRequest request) {
        log.info("start exec method [getSelfOrgFromWorkOrder]. workOrderId:{}", workOrderId);
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        List<RspOrgVO> orgVOList = orderService.getSelfOrg(accountCommon.getPlatform(), accountCommon.getId(), workOrderId);
        BaseResponse response = new BaseResponse(ConstantCode.SUCCESS, orgVOList);
        log.info("success exec method [getSelfOrgFromWorkOrder]. result:{}", JsonUtils.objToString(response));
        return response;
    }


    /**
     * @param workOrderId
     * @return
     */
    @GetMapping(value = "/{workOrderId}/header")
    public BaseResponse getHeaderOfWorkOrder(@PathVariable("workOrderId") Integer workOrderId) {
        log.info("start exec method [getHeaderOfWorkOrder]. workOrderId:{}", workOrderId);
        RspHeaderOfWorkOrderVO header = orderService.getHeaderOfWorkOrder(workOrderId);
        BaseResponse response = new BaseResponse(ConstantCode.SUCCESS, header);
        log.info("success exec method [getHeaderOfWorkOrder]. result:{}", JsonUtils.objToString(response));
        return response;
    }


    /**
     * @param workOrderId
     * @param orgTypeFilter: 0-查所有 1-查peer，2-查orderer  ,默认0
     * @return
     */
    @GetMapping(value = "/{workOrderId}/org-list")
    public BaseResponse listOrgByWorkOrderId(@PathVariable("workOrderId") Integer workOrderId,
                                             @RequestParam(value = "orgTypeFilter", required = false, defaultValue = "0") Byte orgTypeFilter) {
        log.info("start exec method [listOrgByWorkOrderId]. workOrderId:{} orgTypeFilter:{}", workOrderId, orgTypeFilter);
        List<RspOrgOfWorkerOrderVO> orgList = orderService.listOrgByWorkOrderId(workOrderId, orgTypeFilter);
        BaseResponse response = new BaseResponse(ConstantCode.SUCCESS, orgList);
        log.info("success exec method [listOrgByWorkOrderId]. result:{}", JsonUtils.objToString(response));
        return response;
    }

    /**
     * @param workOrderId
     * @return
     */
    @GetMapping(value = "/{workOrderId}/host-list")
    public BaseResponse listHostByWorkOrderId(@PathVariable("workOrderId") Integer workOrderId) {
        log.info("start exec method [listHostByWorkOrderId]. workOrderId:{}", workOrderId);
        List<RspHostVO> hostList = orderService.listHostFromWorkOrder(workOrderId);
        BaseResponse response = new BaseResponse(ConstantCode.SUCCESS, hostList);
        log.info("success exec method [listHostByWorkOrderId]. result:{}", JsonUtils.objToString(response));
        return response;
    }


    /**
     * @param workOrderId
     * @return
     */
    @DeleteMapping(value = "/{workOrderId}")
    public BaseResponse deleteByWorkOrderId(@PathVariable("workOrderId") Integer workOrderId, HttpServletRequest request) {
        log.info("start exec method [deleteByWorkOrderId]. workOrderId:{}", workOrderId);
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        orderService.deleteByWorkOrderId(accountCommon.getAccount(), workOrderId);
        BaseResponse response = BaseResponse.initSuccess();
        log.info("success exec method [deleteByWorkOrderId]. result:{}", JsonUtils.objToString(response));
        return response;
    }

}
